package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_admin_api/biz/model"
)

type UserPointsLogDAO struct {
	bizID int64
}

type UserPointsLogListParams struct {
	Condition []gen.Condition
	Page      int
	Size      int
	Sort      []field.Expr
}

func newUserPointsLogDAO(bizID int64) *UserPointsLogDAO {
	return &UserPointsLogDAO{
		bizID: bizID,
	}
}

func (c *UserPointsLogDAO) MGetUserPointsLog(ctx context.Context, ids []int64) (map[int64]*model.UserPointsLog, error) {
	list, err := UserPointsLog.WithContext(ctx).Where(
		UserPointsLog.BizID.Eq(c.bizID),
		UserPointsLog.ID.In(ids...),
		UserPointsLog.DeletedAt.IsNull(),
	).Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.MGetUserPointsLog err: %v", err)
		return nil, err
	}
	var data = make(map[int64]*model.UserPointsLog)
	for _, info := range list {
		data[info.ID] = info
	}
	return data, nil
}

func (c *UserPointsLogDAO) GetUserPointsLogByConditions(ctx context.Context, condition []gen.Condition) (*model.UserPointsLog, error) {
	tx := UserPointsLog.WithContext(ctx).Where(
		UserPointsLog.BizID.Eq(c.bizID),
		UserPointsLog.DeletedAt.IsNull(),
	)
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	info, err := tx.First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetUserPointsLogByConditions failed, %s", err.Error())
		return nil, err
	}
	return info, nil
}

func (c *UserPointsLogDAO) GetUserPointsLogList(ctx context.Context, condition []gen.Condition, page, size int) ([]*model.UserPointsLog, error) {
	tx := UserPointsLog.WithContext(ctx).Where(UserPointsLog.BizID.Eq(c.bizID), UserPointsLog.DeletedAt.IsNull())
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	if page > 0 && size > 0 {
		tx = tx.Offset((page - 1) * size).Limit(size)
	}
	list, err := tx.Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetUserPointsLogList err: %v", err)
		return nil, err
	}
	return list, nil
}

func (c *UserPointsLogDAO) GetUserPointsLogTotal(ctx context.Context, condition []gen.Condition) (int64, error) {
	total, err := UserPointsLog.WithContext(ctx).Where(UserPointsLog.BizID.Eq(c.bizID), UserPointsLog.DeletedAt.IsNull()).Where(condition...).Count()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetUserPointsLogTotal error: %v", err)
		return 0, err
	}
	return total, nil
}

func (c *UserPointsLogDAO) GetUserPointsLogInfoByID(ctx context.Context, id int64) (*model.UserPointsLog, error) {
	info, err := UserPointsLog.WithContext(ctx).Where(UserPointsLog.BizID.Eq(c.bizID), UserPointsLog.DeletedAt.IsNull()).Where(UserPointsLog.ID.Eq(id)).First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetUserPointsLogInfoByID error: %v", err)
		return nil, err
	}
	return info, nil
}

func (c *UserPointsLogDAO) GetListWithTotal(ctx context.Context, params *UserPointsLogListParams) ([]*model.UserPointsLog, int64, error) {
	tx := UserPointsLog.WithContext(ctx).Where(UserPointsLog.BizID.Eq(c.bizID), UserPointsLog.DeletedAt.IsNull())
	if len(params.Condition) > 0 {
		tx = tx.Where(params.Condition...)
	}
	if len(params.Sort) > 0 {
		tx = tx.Order(params.Sort...)
	}
	list, total, err := tx.FindByPage((params.Page-1)*params.Size, params.Size)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UserPointsLog.GetListWithTotal err: %v", err)
		return nil, 0, err
	}
	return list, total, nil
}

func (c *UserPointsLogDAO) CreateUserPointsLog(ctx context.Context, data *model.UserPointsLog) (*model.UserPointsLog, error) {
	if err := UserPointsLog.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateUserPointsLog error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *UserPointsLogDAO) CreateUserPointsLogWithTx(ctx context.Context, tx *Query, data *model.UserPointsLog) (*model.UserPointsLog, error) {
	if err := tx.UserPointsLog.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateUserPointsLog error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *UserPointsLogDAO) BatchCreateUserPointsLog(ctx context.Context, data []*model.UserPointsLog) error {
	if err := UserPointsLog.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateInBatchesUserPointsLog error: %v", err)
		return err
	}
	return nil
}

func (c *UserPointsLogDAO) BatchCreateUserPointsLogWithTx(ctx context.Context, tx *Query, data []*model.UserPointsLog) ([]*model.UserPointsLog, error) {
	if err := tx.UserPointsLog.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.BatchCreateWithTxUserPointsLog error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *UserPointsLogDAO) UpdateUserPointsLog(ctx context.Context, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := UserPointsLog.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateUserPointsLog error: %v", err)
		return err
	}
	return nil
}

func (c *UserPointsLogDAO) UpdateUserPointsLogWithTx(ctx context.Context, tx *Query, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := tx.UserPointsLog.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateUserPointsLog error: %v", err)
		return err
	}
	return nil
}

func (c *UserPointsLogDAO) DeleteUserPointsLog(ctx context.Context, condition []gen.Condition) error {
	_, err := UserPointsLog.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteUserPointsLog error: %v", err)
		return err
	}
	return nil
}

func (c *UserPointsLogDAO) DeleteUserPointsLogWithTx(ctx context.Context, tx *Query, condition []gen.Condition) error {
	_, err := tx.UserPointsLog.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteUserPointsLogWithTx error: %v", err)
		return err
	}
	return nil
}
